package com.fengba.nanzhi.tabbar04.wz;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;

import com.fengba.nanzhi.fengba.R;
import com.fengba.nanzhi.utils.OKManager;
import com.fengba.nanzhi.utils.Preferences;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class MyTeamLevelActivity extends AppCompatActivity {
    private  String Levelid,user_id;
    private TabHost tabHost;//声明TabHost组件的对象
    private RecyclerView MyTeamLevel1Tab,MyTeamLevel2Tab,MyTeamLevel3Tab;
    private ImageButton MyTeamLevelfh;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.wz_activity_my_team_level);
        Intent intent = getIntent();
        Levelid = intent.getStringExtra("MyLevel_id");
        init1();
        init2();
        init3();
        xuanxiangka();



        final RefreshLayout refreshLayout1 = (RefreshLayout)findViewById(R.id.MyTeamLevel1Layout);
        refreshLayout1.setOnRefreshListener(new OnRefreshListener() {
            public void onRefresh(RefreshLayout refreshlayout) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        init1();
                        Toast.makeText(MyTeamLevelActivity.this, "查询成功", Toast.LENGTH_SHORT).show();
                        refreshLayout1.finishRefresh();
                        /**
                         * 刷新完成后调用此方法，要不然刷新效果不消失
                         */

                    }
                }, 0);
            }
        });

        final RefreshLayout refreshLayout2 = (RefreshLayout)findViewById(R.id.MyTeamLevel2Layout);
        refreshLayout2.setOnRefreshListener(new OnRefreshListener() {
            public void onRefresh(RefreshLayout refreshlayout) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        init2();
                        Toast.makeText(MyTeamLevelActivity.this, "查询成功", Toast.LENGTH_SHORT).show();
                        refreshLayout2.finishRefresh();
                        /**
                         * 刷新完成后调用此方法，要不然刷新效果不消失
                         */

                    }
                }, 0);
            }
        });

        final RefreshLayout refreshLayout3 = (RefreshLayout)findViewById(R.id.MyTeamLevel3Layout);
        refreshLayout3.setOnRefreshListener(new OnRefreshListener() {
            public void onRefresh(RefreshLayout refreshlayout) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        init3();
                        Toast.makeText(MyTeamLevelActivity.this, "查询成功", Toast.LENGTH_SHORT).show();
                        refreshLayout3.finishRefresh();
                        /**
                         * 刷新完成后调用此方法，要不然刷新效果不消失
                         */

                    }
                }, 0);
            }
        });
        MyTeamLevelfh = findViewById(R.id.MyTeamLevelfh);
        MyTeamLevelfh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }


    private void updateTab(final TabHost tabHost) {
        for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
            View view = tabHost.getTabWidget().getChildAt(i);
            TextView tv = (TextView) tabHost.getTabWidget().getChildAt(i).findViewById(android.R.id.title);

            // tv.setTextColor(Color.rgb(255, 255, 255));
            //  TextView tv = tabHost.getTabWidget().getChildAt(i).findViewById(R.id.cardtab);
            if (tabHost.getCurrentTab() == i) {
                tv.setTextSize(14);
                tv.setTypeface(Typeface.SERIF, 0);
                //选中
                view.setBackground(getResources().getDrawable(R.drawable.card_xz));//选中后的背景
                tv.setTextColor(this.getResources().getColorStateList(R.color.colorcheng));
            } else {
                tv.setTextSize(12);
                tv.setTypeface(Typeface.SERIF, 0);
                //不选中
                view.setBackground(getResources().getDrawable(R.drawable.card_wxz));//非选择的背景.
                tv.setTextColor(this.getResources().getColorStateList(android.R.color.black));

            }
        }
    }

    public void init1 (){
        Object user_info = Preferences.getParam(MyTeamLevelActivity.this,"user_info","");
        try {
            JSONObject myJson = new JSONObject(user_info.toString());
            //账号
            user_id = myJson.get("id").toString();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        OKManager.getInstance().type1018(user_id, Levelid.trim(), "4", new OKManager.func4() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                try {
                    if(jsonObject.get("code").toString().equals("0")){
                        JSONArray jsonArray = (JSONArray) jsonObject.get("data");
                        MyTeamLevel1Tab = findViewById(R.id.MyTeamLevel1Tab);
                        MyTeamLevel1Tab.setLayoutManager(new LinearLayoutManager(MyTeamLevelActivity.this));
                        MyTeamLevel1Tab.setAdapter(new MyTeamLevelAdapter(MyTeamLevelActivity.this,jsonArray));
                    }else{
                        showToast(MyTeamLevelActivity.this,jsonObject.get("message").toString());
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(String string) {
                showToast(MyTeamLevelActivity.this,"网络错误");
            }
        });
    }

    public void init2 (){
        OKManager.getInstance().type1018(user_id, Levelid.trim(), "3", new OKManager.func4() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                try {
                    if(jsonObject.get("code").toString().equals("0")){
                        JSONArray jsonArray = (JSONArray) jsonObject.get("data");
                        MyTeamLevel2Tab = findViewById(R.id.MyTeamLevel2Tab);
                        MyTeamLevel2Tab.setLayoutManager(new LinearLayoutManager(MyTeamLevelActivity.this));
                        MyTeamLevel2Tab.setAdapter(new MyTeamLevelAdapter(MyTeamLevelActivity.this,jsonArray));
                    }else{
                        showToast(MyTeamLevelActivity.this,jsonObject.get("message").toString());
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(String string) {
                showToast(MyTeamLevelActivity.this,"网络错误");
            }
        });
    }
    public void init3 (){
        OKManager.getInstance().type1018(user_id, Levelid.trim(), "0", new OKManager.func4() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                try {
                        JSONArray jsonArray = (JSONArray) jsonObject.get("data");
                        MyTeamLevel3Tab = findViewById(R.id.MyTeamLevel3Tab);
                        MyTeamLevel3Tab.setLayoutManager(new LinearLayoutManager(MyTeamLevelActivity.this));
                        MyTeamLevel3Tab.setAdapter(new MyTeamLevelAdapter(MyTeamLevelActivity.this,jsonArray));
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(String string) {
                showToast(MyTeamLevelActivity.this,"网络错误");
            }
        });
    }
    public static void showToast(final Activity ctx, final String msg) {
        // 判断是在子线程，还是主线程
        if ("main".equals(Thread.currentThread().getName())) {
            Toast.makeText(ctx, msg, Toast.LENGTH_SHORT).show();
        } else {
            // 子线程
            ctx.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(ctx, msg, Toast.LENGTH_SHORT).show();
                }
            });
        }
    }

        /**
         * TabHost选择监听器
         * @author
         *
         */
        private class tabChangedListener implements TabHost.OnTabChangeListener {

            @Override
            public void onTabChanged(String tabId)
            {
                tabHost.setCurrentTabByTag(tabId);
                updateTab(tabHost);
            }
        }

        public void xuanxiangka(){
            tabHost=(TabHost)findViewById(android.R.id.tabhost);//获取tabHost对象
            tabHost.setup();//初始化TabHost组件

            LayoutInflater inflater=LayoutInflater.from(this);//声明并实例化一个LayoutInflater对象
            //关于LayoutInflater详细，请看我的另外一篇转载的总结
            inflater.inflate(R.layout.wz_activity_my_team_level1, tabHost.getTabContentView());
            inflater.inflate(R.layout.wz_activity_my_team_level2, tabHost.getTabContentView());
            inflater.inflate(R.layout.wz_activity_my_team_level3, tabHost.getTabContentView());

            tabHost.addTab(tabHost.newTabSpec("tab01")
                    .setIndicator("全部")
                    .setContent(R.id.My_Team_Level1));//添加第一个标签页
            tabHost.addTab(tabHost.newTabSpec("tab02")
                    .setIndicator("已认证")
                    .setContent(R.id.My_Team_Level2));//添加第二个标签页
            tabHost.addTab(tabHost.newTabSpec("tab03")
                    .setIndicator("未认证")
                    .setContent(R.id.My_Team_Level3));//添加第二个标签页
            updateTab(tabHost);
            tabHost.setOnTabChangedListener(new MyTeamLevelActivity.tabChangedListener());
        }
}
